﻿// -----------------------------------------------------------------------
// <copyright file="DmNhaCungCapDataAccess.cs" company="">
// TODO: Copyright by dangppsoft@gmail.com.
// </copyright>
// -----------------------------------------------------------------------
namespace ManagerClinic.Core.DataAccess
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using ManagerClinic.Core.Common;
    using System.Data.Common;
    using System.Data;
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using Microsoft.Practices.EnterpriseLibrary.Common;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
    using ManagerClinic.Ultilities.Ultility;

    public class DmNhaCungCapDataAccess
    {
        public bool InsertDmNhaCungCap(DmNhaCungCapEntity obj)
        {
            try
            {
                string sql = "spDmNhaCungCap_Insert";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaNhaCungCap", DbType.String, obj.MaNhaCungCap);
                db.AddInParameter(cmd, "TenNhaCungCap", DbType.String, obj.TenNhaCungCap);
                db.AddInParameter(cmd, "DiaChi", DbType.String, obj.DiaChi);
                db.AddInParameter(cmd, "DienThoai", DbType.String, obj.DienThoai);
                db.AddInParameter(cmd, "Fax", DbType.String, obj.Fax);
                db.AddInParameter(cmd, "Email", DbType.String, obj.Email);
                db.AddInParameter(cmd, "Website", DbType.String, obj.Website);
                db.AddInParameter(cmd, "MoTa", DbType.String, obj.MoTa);
                db.AddInParameter(cmd, "TrangThaiSuDung", DbType.Boolean, obj.TrangThaiSuDung);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("InsertDmNhaCungCap", ex.Message); throw ex; }
        }

        public bool EditDmNhaCungCap(DmNhaCungCapEntity obj)
        {
            try
            {
                string sql = "spDmNhaCungCap_Update";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaNhaCungCap", DbType.String, obj.MaNhaCungCap);
                db.AddInParameter(cmd, "TenNhaCungCap", DbType.String, obj.TenNhaCungCap);
                db.AddInParameter(cmd, "DiaChi", DbType.String, obj.DiaChi);
                db.AddInParameter(cmd, "DienThoai", DbType.String, obj.DienThoai);
                db.AddInParameter(cmd, "Fax", DbType.String, obj.Fax);
                db.AddInParameter(cmd, "Email", DbType.String, obj.Email);
                db.AddInParameter(cmd, "Website", DbType.String, obj.Website);
                db.AddInParameter(cmd, "MoTa", DbType.String, obj.MoTa);
                db.AddInParameter(cmd, "TrangThaiSuDung", DbType.Boolean, obj.TrangThaiSuDung);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("UpdateDmNhaCungCap", ex.Message); throw ex; }
        }

        public bool DeleteDmNhaCungCap(string maNhaCungCap)
        {
            try
            {
                string sql = "spDmNhaCungCap_DeleteByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaNhaCungCap", DbType.String, maNhaCungCap);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("DeleteDmNhaCungCap", ex.Message); throw ex; }
        }

        public DmNhaCungCapEntity GetDmNhaCungCap(string maNhaCungCap)
        {
            try
            {
                DmNhaCungCapEntity obj = null;
                string sql = "spDmNhaCungCap_SelectByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaNhaCungCap", DbType.String, maNhaCungCap);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new DmNhaCungCapEntity();
                        obj.MaNhaCungCap = reader["MaNhaCungCap"].ToString();
                        obj.TenNhaCungCap = reader["TenNhaCungCap"].ToString();
                        obj.DiaChi = reader["DiaChi"].ToString();
                        obj.DienThoai = reader["DienThoai"].ToString();
                        obj.Fax = reader["Fax"].ToString();
                        obj.Email = reader["Email"].ToString();
                        obj.Website = reader["Website"].ToString();
                        obj.MoTa = reader["MoTa"].ToString();
                        obj.TrangThaiSuDung = String.IsNullOrEmpty(reader["TrangThaiSuDung"].ToString()) == true ? false : (bool)reader["TrangThaiSuDung"];
                    }
                }
                return obj;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetDmNhaCungCap", ex.Message); throw ex; }
        }

        public List<DmNhaCungCapEntity> GetDmNhaCungCapList()
        {
            try
            {
                List<DmNhaCungCapEntity> list = new List<DmNhaCungCapEntity>();
                DmNhaCungCapEntity obj = null;
                string sql = "spDmNhaCungCap_SelectAll";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new DmNhaCungCapEntity();
                        obj.MaNhaCungCap = reader["MaNhaCungCap"].ToString();
                        obj.TenNhaCungCap = reader["TenNhaCungCap"].ToString();
                        obj.DiaChi = reader["DiaChi"].ToString();
                        obj.DienThoai = reader["DienThoai"].ToString();
                        obj.Fax = reader["Fax"].ToString();
                        obj.Email = reader["Email"].ToString();
                        obj.Website = reader["Website"].ToString();
                        obj.MoTa = reader["MoTa"].ToString();
                        obj.TrangThaiSuDung = String.IsNullOrEmpty(reader["TrangThaiSuDung"].ToString()) == true ? false : (bool)reader["TrangThaiSuDung"];
                        list.Add(obj);
                    }
                }
                return list;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetDmNhaCungCapList", ex.Message); throw ex; }
        }

    }
}